//
//  ViewController.swift
//  LXSwift
//
//  Created by Jackabc1011 on 12/18/2024.
//  Copyright (c) 2024 Jackabc1011. All rights reserved.
//

import UIKit
import LXSwift

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.random
        
        
        let dateUtils = DateUtils.shared // 获取单例
        
        // 设置日期格式
        dateUtils.setDateFormat("yyyy-MM-dd HH:mm:ss")
        dateUtils.setTimeZone("Asia/Shanghai") //设置时区
        dateUtils.setLocale("zh_CN") //设置locale
        
        // 字符串转 Date
        if let date = dateUtils.date(from: "2024-08-21 12:30:00") {
            print("Date: \(date)")
            
            // Date 转字符串
            let dateString = dateUtils.string(from: date)
            print("Date String: \(dateString)")
            
            // Date 转时间戳
            let timestamp = dateUtils.timestamp(from: date)
            print("Timestamp: \(timestamp)")
            
            let milliTimestamp = dateUtils.milliTimestamp(from: date)
            print("Milli Timestamp: \(milliTimestamp)")
            
        }
        
        // 时间戳转 Date
        let timestamp: TimeInterval = 1692604200
        let dateFromTimestamp = dateUtils.date(from: timestamp)
        print("Date from timestamp: \(dateFromTimestamp)")
        
        let milliTimeStamp: TimeInterval = 1692604200000
        let dateFromMilliTimestamp = dateUtils.dateFromMilli(from: milliTimeStamp)
        print("Date from milliTimestamp: \(dateFromMilliTimestamp)")
        
        let date1 = Date()
        let date2 = Date(timeIntervalSinceNow: 3600) // 一小时后的时间
        
        // 比较日期
        let comparisonResult = dateUtils.compare(date1: date1, date2: date2)
        switch comparisonResult {
        case .orderedAscending:
            print("date1 < date2")
        case .orderedDescending:
            print("date1 > date2")
        case .orderedSame:
            print("date1 == date2")
        }
        
        //判断是否为同一天
        let isSameDay = dateUtils.isSameDay(date1: date1, date2: date2)
        print("isSameDay:\(isSameDay)")
        
        // 日期加减
        if let tomorrow = dateUtils.date(byAdding: .day, value: 1, to: date1) {
            print("Tomorrow: \(tomorrow)")
        }
        
        // 获取日期组成部分
        let year = dateUtils.component(.year, from: date1)
        print("Year: \(year)")
        
        // 获取两个日期的时间间隔
        let components = dateUtils.dateComponents([.hour, .minute, .second], from: date1, to: date2)
        print("相差\(components.hour ?? 0)小时\(components.minute ?? 0)分钟\(components.second ?? 0)秒")
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
}

